package com.datn.DataAccess;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.datn.beans.InstructionBean;
import com.datn.utils.FileUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class InstructionDatabaseAdapter {

	public static final String TAG ="UserDatabaseAdapter";

	public static final String KEY_ID_INSTRUCTION = "IdInstruction";
	public static final String KEY_ID_PLACE = "IdPlace";
	public static final String KEY_STEP = "Step";
	public static final String KEY_INSTRUCTION = "Instruction";
	//HERE


	private static final String DATABASE_NAME = "NearLocationDatabase";
	private static final String DATABASE_TABLE = "tblInstruction";
	private static final int DATABASE_VERSION = 6;

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDB;

	private final Context mContext;



	public InstructionDatabaseAdapter(Context ctx){
		this.mContext = ctx;
	}

	public InstructionDatabaseAdapter open()
	{		
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
		mDB = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close(){
		mDbHelper.close();
	}

	private Cursor getListInstructionCursorWithPlaceId(String PlaceId){
		return mDB.query(DATABASE_TABLE, new String[] {KEY_ID_INSTRUCTION, KEY_ID_PLACE, KEY_STEP, KEY_INSTRUCTION}, KEY_ID_PLACE+"=?", new String[]{PlaceId}, null, null, null);
	}

	public ArrayList<InstructionBean> getListInstructionObjectWithPlaceId(String PlaceId){
		Cursor cursor = getListInstructionCursorWithPlaceId(PlaceId);
		ArrayList<InstructionBean> listInstruction = new ArrayList<InstructionBean>();
		if (cursor!=null){
			try {

				while (cursor.moveToNext()){
					InstructionBean bean = new InstructionBean();
					int id_instruction = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID_INSTRUCTION));
					int id_place = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID_PLACE));
					int step = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_STEP));
					String stInstruction = cursor.getString(cursor.getColumnIndexOrThrow(KEY_INSTRUCTION));
					
					bean.setInstructionId(id_instruction);
					bean.setPlaceId(id_place);
					bean.setStep(step);
					bean.setInstruction(stInstruction);
					
					listInstruction.add(bean);
				}


			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		return listInstruction;
	}
	
	
	public ArrayList<String> getListInstructionWithPlaceId(String placeId){
		Cursor cursor = getListInstructionCursorWithPlaceId(placeId);
		ArrayList<String> listInstruction = new ArrayList<String>();
		if (cursor!=null){
			try {

				while (cursor.moveToNext()){
					String stInstruction = cursor.getString(cursor.getColumnIndexOrThrow(KEY_INSTRUCTION));
					listInstruction.add(stInstruction);
				}

			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		return listInstruction;
	}

	public long createInstruction(String IdPlace, List<String> allInstruction){
		long result = -1;
		if (allInstruction!=null){
			for (int i = 0; i < allInstruction.size(); i++) {
				ContentValues inititalValues = new ContentValues();
				inititalValues.put(KEY_ID_PLACE, IdPlace);
				inititalValues.put(KEY_STEP, i);
				inititalValues.put(KEY_INSTRUCTION, allInstruction.get(i));
				result = mDB.insert(DATABASE_TABLE, null, inititalValues);
				if (result == -1) break;
			}
		}			
		return result;
	}






	/*public long createUser(String name){
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(KEY_USERNAME, name);
		return mDB.insert(DATABASE_TABLE, null, inititalValues);
	}

	public boolean deleteUser(long rowId)
	{
		return mDB.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) >0;
	}
	public Cursor getAllUsers(){
		return mDB.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME}, null, null, null, null, null);
	}*/
}

